﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;

namespace SmartHotel.MVC.MyFilter
{
    public class LoginFilter : Attribute, IAuthorizationFilter
    {
        public void OnAuthorization(AuthorizationFilterContext context)
        {
            //检查cookie
            var cookies = context.HttpContext.Request.Cookies;
            if (cookies.Any(x => x.Key.Equals("username")))
            {
                //如果cookies里存在username,就代表登录了.
            }
            else {
                //说明没有登录,因为cookies里没有存登录信息
                context.Result = new RedirectResult("/Member/Login");
            }
        }
    }
}
